package com.changpay.zywy.ui.activity.serivce

import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.changpay.zywy.R
import com.changpay.zywy.adapter.SelectProjectAdapter
import com.changpay.zywy.app.App.Companion.context
import com.changpay.zywy.base.BaseActivity
import com.changpay.zywy.base.IView
import com.changpay.zywy.bean.ProjectBean
import com.changpay.zywy.ext.sss
import com.changpay.zywy.http.RetrofitHelper
import com.changpay.zywy.wight.SpaceItemDecoration
import kotlinx.android.synthetic.main.activity_recycle.*
import kotlinx.android.synthetic.main.title_layout.*

class SelectProjectActivity : BaseActivity(), IView {

    override fun showLoading() {
    }

    override fun hideLoading() {

    }

    override fun showDefaultMsg(msg: String) {
    }

    override fun showMsg(msg: String) {
    }

    override fun showError(errorMsg: String) {
    }

    override fun attachLayoutRes(): Int {
        return R.layout.activity_recycle
    }

    override fun initData() {
    }

    override fun initView() {
        tv_title.text = intent.getStringExtra("title")
        iv_back.setOnClickListener { finish() }
        initRecyclerView()
    }

    override fun start() {
        loadData()
    }

    private fun loadData() {
        // 加载底部数据
        val param = hashMapOf<String, String>()
        param["key"] = token
        RetrofitHelper.service.getProject(param).sss(this)
        {
            mAdapter.addData(it.result)
        }

    }


    /**
     * datas
     */
    private val datas = mutableListOf<ProjectBean>()

    /**
     * RecyclerView Divider
     */
    private val recyclerViewItemDecoration by lazy {
        this.let {
            SpaceItemDecoration(context!!)
        }
    }

    /**
     * Adapter
     */
    private val mAdapter: SelectProjectAdapter by lazy {
        SelectProjectAdapter(context, datas)
    }


    /**
     * LinearLayoutManager
     */
    private val linearLayoutManager: LinearLayoutManager by lazy {
        LinearLayoutManager(context)
    }


    /**
     * ItemClickListener
     */
    private val ItemClickListener = BaseQuickAdapter.OnItemClickListener { _, _, position ->
        if (intent.type == "0")  //商户费率需改
        {
            Intent(this,SetShopRateActivity::class.java).run {
                putExtra("gc_id",datas[position].gc_id.toString())
                startActivity(this)
            }
        } else if (intent.type == "1") // 结算卡修改
        {
            Intent(this,JieSuanKaChangeActivity::class.java).run {
                putExtra("gc_id",datas[position].gc_id.toString())
                startActivity(this)
            }
        }
        else if (intent.type == "2") // 结算卡修改
        {
            Intent(this, SetLowerPriceActivity::class.java).run {
                putExtra("type", "zhengche")
                putExtra("gc_id",datas[position].gc_id.toString())
                startActivity(this)
            }
        }
        else if (intent.type == "3") // 结算卡修改
        {
            Intent(this,SetRateActivity::class.java).run {
                putExtra("gc_id",datas[position].gc_id.toString())
                startActivity(this)
            }
        }
        else if (intent.type == "4") // 结算卡修改
        {
            Intent(this,SetAwardActivity::class.java).run {
                putExtra("gc_id",datas[position].gc_id.toString())
                startActivity(this)
            }
        } else if (intent.type == "5") // 结算卡修改
        {
            Intent(this,MachineBackActivity::class.java).run {
                putExtra("gc_id",datas[position].gc_id.toString())
                startActivity(this)
            }
        }
    }


    /**
     * ItemChildClickListener
     */
    private val onItemChildClickListener = BaseQuickAdapter.OnItemChildClickListener { _, view, position ->

    }

    private fun initRecyclerView() {
        recyclerView.run {
            layoutManager = linearLayoutManager
            adapter = mAdapter
//            itemAnimator = DefaultItemAnimator()
//            recyclerViewItemDecoration.let { addItemDecoration(it) }
        }
        mAdapter.run {
            bindToRecyclerView(recyclerView)
            onItemChildClickListener = this@SelectProjectActivity.onItemChildClickListener

//            setOnLoadMoreListener(onRequestLoadMoreListener, recyclerView)
            onItemClickListener = this@SelectProjectActivity.ItemClickListener
        }
    }

}